//
//  FMDBAdManager.m
//  takeout
//
//  Created by Mac on 12-9-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FMDBAdManager.h"
#import "FMDBManager.h"
#import "FMDatabase.h"
#import "Ad.h"
@implementation FMDBAdManager




//插入广告到本地数据
-(void)insertAd:(NSArray *)ads position:(NSString *)position{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    [db executeUpdate:@"DELETE from ad where position=?",position];
    for(int i =0;i<[ads count];i++){   
        Ad *ad=[ads objectAtIndex:i];
       [db executeUpdate:@"INSERT INTO ad(id, type,value,imgurl,position,sort,desc,createtime,updateTime) VALUES(?, ?,?,?,?,?,?,?,?)",[NSNumber numberWithInt:ad.adId],ad.type,ad.value,ad.imgUrl,ad.position,[NSNumber numberWithInt:ad.sort],ad.desc,ad.createTime,ad.updateTime];
       
    }
    

}

//根据广告id查找广告
-(Ad *)getAdForId:(NSString *)adId{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM ad where id=?",adId]; 
    newAd=[[Ad alloc]init];
    while ([rs next]) {
        newAd.adId=[rs intForColumn:@"id"];
        newAd.type=[rs stringForColumn:@"type"];
        newAd.value=[rs stringForColumn:@"value"];
        newAd.imgUrl=[rs stringForColumn:@"imgurl"];
        newAd.position=[rs stringForColumn:@"position"];
        newAd.sort=[rs intForColumn:@"sort"];
        newAd.desc=[rs stringForColumn:@"desc"];
        newAd.createTime=[rs stringForColumn:@"createtime"];
        newAd.updateTime=[rs stringForColumn:@"updatetime"];
    }
    [rs close];


    
    return newAd;
}


//获得本地广告集合
-(NSArray *)getAdForPosition:(NSString *)position{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM ad where position=?",position]; 
    NSMutableArray *ads=[NSMutableArray arrayWithCapacity:3];
    while ([rs next]) {
        Ad *ad=[[Ad alloc]init];
        ad.adId=[rs intForColumn:@"id"];
        ad.type=[rs stringForColumn:@"type"];
        ad.value=[rs stringForColumn:@"value"];
        ad.imgUrl=[rs stringForColumn:@"imgurl"];
        ad.position=[rs stringForColumn:@"position"];
        ad.sort=[rs intForColumn:@"sort"];
        ad.desc=[rs stringForColumn:@"desc"];
        ad.createTime=[rs stringForColumn:@"createtime"];
        ad.updateTime=[rs stringForColumn:@"updatetime"];
        [ads addObject:ad];

    }
    [rs close];


    return ads;
}


//查询所有
-(void) select{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];

    FMResultSet *rs = [db executeQuery:@"SELECT * FROM ad"];
    while ([rs next]){
        NSLog(@"id%@ type%@ %@ %@ %@ %@ %@ %@ %@",[rs stringForColumn:@"id"],[rs stringForColumn:@"type"],[rs stringForColumn:@"value"],[rs stringForColumn:@"imgurl"],[rs stringForColumn:@"position"],[rs stringForColumn:@"sort"],[rs stringForColumn:@"desc"],[rs stringForColumn:@"createtime"],[rs stringForColumn:@"updateTime"]);
    }
    [rs close];


}



@end
